require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UIChaosTestRewardPreview = UIBase.New(PanelNames.UIChaosTestRewardPreview);
local change_type = 1
function UIChaosTestRewardPreview.Init(func, _change_type)
    change_type = _change_type or 1
    UIBase.Init(UIChaosTestRewardPreview, func);
end

--定义控件
function UIChaosTestRewardPreview:OnCreate()
    self.rewardCount = 1;
    self.rewardList = self:GetChild("RewardList");
    self.rankListBtn = self:GetChild("RankListButton");
    self.closeBgBtn = self:GetChild("CloseBg");
    self.typeController = self:GetController("change");
    self:addEvent();
    if change_type == 1 then
        self:RegisterEvent(Config.EventType.Trials_Chaos_Work_Rank, self.refresh);
    else
        -- todo
    end
    --self.CloseFom();
end

function UIChaosTestRewardPreview:OnEnable()
    self:refresh();
end

function UIChaosTestRewardPreview:OnDisable()

end

function UIChaosTestRewardPreview:refresh()
    if change_type == 1 then
        local rankInfoId = logicMgr.TrialsManager.GetMyRankInfo().workRank;
        --local rankInfoId = 1;
        if rankInfoId == 0 then
            self.typeController.selectedIndex = 1;
        else
            self.typeController.selectedIndex = 0;
            
            local cfg_trialsrank = G.dataTable["t_trialsrank"] 
            local num = 0
            for _, _ in pairs(cfg_trialsrank) do
                num = num+1
            end 
            if tonumber(rankInfoId ) >= num then
                self.rewardConfig = decodeJsonStrToTabel(cfg_trialsrank[num].f_Reward)
            else
                self.rewardConfig = decodeJsonStrToTabel(cfg_trialsrank[tostring(rankInfoId)].f_Reward)
            end
            self.rewardCount = 1
            self.rewardList.itemRenderer = self.rendererItem
            self.rewardList.numItems = #self.rewardConfig
        end
    else
        -- todo
    end
end

function UIChaosTestRewardPreview:OnDestroy()
    if change_type == 1 then
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Work_Rank, self.refresh, self)
    else
        -- todo
    end

end

function UIChaosTestRewardPreview:addEvent()
    self:RegisterOnClick(self.rankListBtn, self.OpenRank)
    self:RegisterOnClick(self.closeBgBtn, self.onCloseClick)
end

function UIChaosTestRewardPreview:OpenRank()
    showUI(PanelNames.UIChaosTestRank, nil, change_type)
end

function UIChaosTestRewardPreview:removeEvent()
    ----bt_SetBtn.onClick:Clear();
    --self.backBtn.onClick:Clear();
    --self.bg.onClick:Clear();
    --self.lastBtn.onClick:Clear();
    --self.nextBtn.onClick:Clear();
    --self.titleDecBackBtn.onClick:Clear();
end

function UIChaosTestRewardPreview:onCloseClick()
    closeUI(PanelNames.UIChaosTestRewardPreview)
end

function UIChaosTestRewardPreview:rendererItem(obj)
    local self = UIChaosTestRewardPreview
    if change_type == 1 then
        local info = self.rewardConfig[self.rewardCount];
        local data = logicMgr.ItemManager.getItemConstDatabyModelId(info[1]);
        local power = logicMgr.EcoBuffManager.getEcoBuffByTypeAndItemId(Config.EcoBuffCondition.ChaosTestReward, info[1]);
        data.num = Mathf.Round(info[2] * (1 + power * 0.0001));
        local itemCtr = CtrlManager.GetCtrl(PanelNames.Item);
        itemCtr.initData(obj, data);
        self.rewardCount = self.rewardCount + 1
    else
        -- todo
    end
end

return UIChaosTestRewardPreview;